﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    CategoryController cateControl;
    DanhmucsanphamController danhmucsanphamControl;
    DanhmucPhukienController danhmucphukienControl;
    ProductsController productControl;

    DataTable dtCate;
    DataTable dtEnableCate;
    DataTable dtDanhmucsanpham;
    DataTable dtDanhmucphukien;
    DataTable dtMostView;
    DataTable testview;
    DataTable dtProduct=new DataTable();

    public List<Category> listCate=new List<Category>();
    public List<DanhmucPhukien> listDanhmucphukien=new List<DanhmucPhukien>();
    public List<Danhmucsanpham> listDanhmucsanpham=new List<Danhmucsanpham>();

    protected void Page_Load(object sender, EventArgs e)
    {
        cateControl = new CategoryController();
        danhmucphukienControl = new DanhmucPhukienController();
        danhmucsanphamControl = new DanhmucsanphamController();
        productControl = new ProductsController();

        dtCate = cateControl.getAllCategory();
        dtEnableCate = cateControl.getEnableCategory();
        dtDanhmucphukien = danhmucphukienControl.GetAll();
        dtDanhmucsanpham = danhmucsanphamControl.GetAll();

        LoadCate();
        LoadDanhmucphukien();
        LoadDanhmucsanpham();
        LoadMostView();

        //dtlTest.DataSource = testview;
        //dtlTest.DataBind();
        parentDataList.DataSource = dtEnableCate;
        parentDataList.DataBind();
    }

    public void LoadCate() {
        foreach (DataRow item in dtCate.Rows)
        {
            listCate.Add(new Category(item));
        }
    }
    public void LoadDanhmucphukien()
    {
        foreach (DataRow item in dtDanhmucphukien.Rows)
        {
            listDanhmucphukien.Add(new DanhmucPhukien(item));
        }
    }
    public void LoadDanhmucsanpham()
    {
        foreach (DataRow item in dtDanhmucsanpham.Rows)
        {
            listDanhmucsanpham.Add(new Danhmucsanpham(item));
        }
    }
    public void LoadMostView() {
        dtMostView = productControl.Get_Most_View();
        dtlMostView.DataSource = dtMostView;
        dtlMostView.DataBind();
    }

    protected void parentDataList_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        DataRowView dtrv = (DataRowView)e.Item.DataItem;
        int id = (int)dtrv.Row["category_id"];
        testview = productControl.GetTopProductByCateId(id);
        DataList dl = (DataList)e.Item.FindControl("dtlTest");
        dl.DataSource = testview;
        dl.DataBind();
    }
    protected void dtlTest_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        DataRowView dtrv = (DataRowView)e.Item.DataItem;
        int id = (int)dtrv.Row["product_id"];
        dtProduct = productControl.GetProductProperty(id);
        DataList dl = (DataList)e.Item.FindControl("dtlTest1");
        dl.DataSource = dtProduct;
        dl.DataBind();
    }
}
